package com.itheima.aimobile.tool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.aimobile.domain.*;
import com.itheima.aimobile.mapper.MobileMapper;
import com.itheima.aimobile.service.IOrderInfoService;
import com.itheima.aimobile.service.IPackageInfoService;
import com.itheima.aimobile.service.IUserInfoService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class MyTool {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IPackageInfoService packageInfoService;
    @Autowired
    private IUserInfoService userInfoService;

    //查询套餐详情
    @Tool(description = "可以根据条件查询所有套餐详情")
    public List<PackageInfo> getPackageInfo(@ToolParam(required = false,description = "查询条件") PackageInfoQuery query) {
        //如果条件为空，则查询所有
        return packageInfoService.list();
    }
    @Autowired
    private MobileMapper mobileMapper;
    //查询所有套餐详情
    @Tool(description = "查询所有套餐详情")
    public List<Mobile> getMobile() {
        return mobileMapper.selectList( null);
    }

    //保存用户信息
    @Tool(description = "保存用户信息")
    public void saveUser(@ToolParam (description = "手机号") String phone,
                               @ToolParam (description = "用户名") String username) {
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setUsername(username);
        userInfoService.save(userInfo);
    }

    //保存用户信息和订单，并返回订单编号
    @Tool(description = "保存用户信息和订单，并w且返回订单编号")
    @Transactional(rollbackFor = Exception.class)
    public String saveUserInfo(@ToolParam (description = "手机号") String phone,
                               @ToolParam (description = "用户名") String username,
                               @ToolParam (description = "套餐名称") String packageName) {
        //把用户信息保存到用户表内
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setUsername(username);
        userInfoService.save(userInfo);
        //把用户想要定的套餐信息保存到订单表内
        //现根据传来的套餐名称查询套餐信息
        PackageInfo packageInfo = packageInfoService.getOne(new QueryWrapper<PackageInfo>().eq("package_name", packageName));
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userInfo.getId());
        orderInfo.setPackageId(packageInfo.getId());
        orderInfoService.save(orderInfo);
        return orderInfo.getId().toString();
    }

}
